/* 
 * @description:自定义SliverAppBar
 * @author: Jane 
 * @date: 2023-10-11 16:55:46
 */
import 'package:basic_widgets/build_sliver_appbar.dart';
import 'package:basic_widgets/no_shadow_behavior.dart';
import 'package:flutter/material.dart';

class SliverCustomAppBar extends StatefulWidget {
  const SliverCustomAppBar({
    Key? key,
    required this.height,
    required this.cover,
    required this.coverHeight,
    required this.radius,
    required this.bgChild,
    required this.child,
    required this.slivers,
    required this.title,
    required this.scrollController,
    required this.bgColor,
  }) : super(key: key);

  final double height;
  final String cover;
  final double coverHeight;
  final double radius;

  final Widget child;
  final Widget bgChild;
  final Widget title;
  final ScrollController scrollController;

  final List<Widget> slivers;
  final Color bgColor;

  @override
  State<SliverCustomAppBar> createState() => _SliverCustomAppBarState();
}

class _SliverCustomAppBarState extends State<SliverCustomAppBar>
    with SingleTickerProviderStateMixin {
  late AnimationController animationController;
  late Animation<double> animation;

  double startDy = 0;
  double expendHeight = 0;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 300),
    );
    animation = Tween(begin: 0.0, end: 0.0).animate(animationController);
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Listener(
        onPointerMove: (e) {
          // 记录初始位置的信息
          if (startDy == 0) startDy = e.position.dy;
          // 滑动的距离 = 滑动到的位置 - 初始位置（防止滑动太长所以除以3）
          expendHeight = (e.position.dy - startDy) / 3;
          setState(() {});
        },
        onPointerUp: (e) {
          startDy = 0;
          animation =
              Tween(begin: expendHeight, end: 0.0).animate(animationController)
                ..addListener(() {
                  expendHeight = animation.value;
                  setState(() {});
                });
          animationController.forward(from: 0.0);
        },
        child: ScrollConfiguration(
          // 防止下拉时顶部出现水波纹效果
          behavior: NoShadowScrollBehavior(),
          // TODO:实际应用中大部分应该改为NestedScrollView
          child: CustomScrollView(
            controller: widget.scrollController,
            slivers: [
              BuildSliverAppBar(
                expendHeight: expendHeight,
                height: widget.height,
                cover: widget.cover,
                coverHeight: widget.coverHeight,
                radius: widget.radius,
                bgChild: widget.bgChild,
                title: widget.title,
                bgColor: widget.bgColor,
                child: widget.child,
              ),
              ...widget.slivers,
            ],
          ),
        ),
      ),
    );
  }
}
